<?php
	
namespace Home\Model;
	
use Think\Model;
	
class IndexModel extends Model
{

	public function _before_insert(&$data,$options){
		//echo __METHOD__;exit;
		$this->UploadsImg($data,$options);
		
	}

	//封装文件上传函数
	public function UploadsImg(&$data,$options){
		echo __METHOD__;exit;
		//0+exit;
		$iniFileSize = intval(ini_get('upload_max_filesize'));//程序配置
		//dump($iniFileSize);exit;
		//获取php.ini
		//配置文件允许上传大小
		$localFileSize = intval(C('MAX_FILE_SIZE'));
		//dump($localFileSize);exit;
		$allowFileSize = min($iniFileSize,$localFileSize);
		//dump($allowFileSize);exit;
		//上传的跟路径
		$rootPath = C("ROOT_IMG_PATH");
		//dump($rootPath);exit;
		//允许上传的图片后缀
		$ext = C("ALLOW_IMG_EXT");
		
		$config = array(
				'maxSize'    =>    $allowFileSize * 1024 * 1024,
				'rootPath'   =>    $rootPath,
				'savePath'   =>    'Goods/',
				'exts'       =>    $ext,
		);
		
		$upload = new \Think\Upload($config);
		// dump($upload);exit;
		$info   =   $upload->upload();
		//echo $upload->getError();
		//dump($info);exit;
		if($info) {
			// 上传成功 获取上传文件信息
			$imgName = $info['goods_img']['savepath'].$info['goods_img']['savename'];
			//var_dump($imgName);exit;
			$data['goods_img'] = $imgName;
		
		}else{
			// 上传错误提示错信息
		
		
		}
		
/*		//生成缩略图
		$image = new \Think\Image();
		
		$image->open($rootPath.$imgName);
		
		$thumblmgName = $info['goods_img']['savepath'].'thumb_'.$info['goods_img']['savename'];
		
		//生成缩略图的时候，有三个参数
		//第一个和第二个图片的宽和高
		//第三个参数为图片的缩放比例
		//注意路径后面需要/结尾
		$image->thumb(150,150,6)->save($rootPath.$thumblmgName);
		
		//缩略图路径
		$data['goods_thumb'] = $thumblmgName;*/
		
	} 
}